package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckGroupItemDao;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckGroupItemDao checkGroupItemDao;



    @Transactional
    public void add(CheckGroup checkGroup,Integer[] checkitemIds) {

        checkGroupDao.insert(checkGroup);
        Integer checkGroupId=checkGroupDao.getid(checkGroup.getCode());


        if (checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id",checkGroupId);
                map.put("checkitem_id",checkitemId);
                checkGroupItemDao.insert(map);
            }


        }

    }

    @Override
    public void deleteById(Integer id) {
        //TODO 套餐中如果包含检查组 检查一下
        //删除检查组的信息
        checkGroupDao.deleteById(id);
        //删除检查组检查项关系表中的数据
        checkGroupItemDao.deleteByCheckGroupId(id);

    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.deleteAssociation(checkGroup.getId());
        if (checkitemIds!=null && checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
               Map<String,Integer> map=new HashMap<>();
               map.put("checkgroup_id",checkGroup.getId());
               map.put("checkitem_id",checkitemId);
               checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
        checkGroupDao.edit(checkGroup);
    }

    @Override
    public List<CheckGroup> findAll() {
        log.info("检查组查询所有");
        return checkGroupDao.findAll();
    }


    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean){
        log.info("[检查项-分页查询]data：{}",queryPageBean);
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        // 分页查询数据库
        Page<CheckGroup> page=checkGroupDao.Page(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }
}
